package com.glela.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.glela.common.beans.ParamBean;
import com.glela.common.dao.IBaseDao;
import com.glela.common.service.IBaseService;

/**
 * @author liujie
 * @date 2016年3月4日
 *
 */
public abstract class AbstractBaseService<T> implements IBaseService<T> {

	@Autowired
	protected IBaseDao<T> baseDao;
	
	@Autowired
	protected Mapper<T> mapper;

//	public IBaseDao<T> getBaseDao() {
//		return baseDao;
//	}
	
	public void initBaseDao()
	{
		baseDao.setMapper(mapper);
	}

	@Override
	public T queryByKey(Object key) {
		return baseDao.queryByKey(key);
	}

	@Override
	public int save(T entity) {
		return baseDao.save(entity);
	}

	@Override
	public int remove(Object key) {
		return baseDao.remove(key);
	}

	@Override
	public int updateAll(T entity) {
		return baseDao.updateAll(entity);
	}

	@Override
	public int updateNotNull(T entity) {
		return baseDao.updateNotNull(entity);
	}

	@Override
	public List<T> queryByExample(Object example) {
		return baseDao.queryByExample(example);
	}
	
	@Override
	public List<T> queryByCondition(List<ParamBean> paramBeanList,int page,int rows) {

		Example example = new Example(getClazz());
		Example.Criteria criteria = example.createCriteria();
		if(paramBeanList!=null)
		{
			for (ParamBean paramBean : paramBeanList) {
				if(paramBean==null)
				{
					break;
				}
				if (paramBean.getName() != null&&paramBean.getValue()!=null) {
					criteria.andEqualTo(paramBean.getName(), paramBean.getValue());
				}
			}
		}
		if (rows > 0) {
			PageHelper.startPage(page, rows);
		}
		return queryByExample(example);
	}
	
	protected Class<?> getClazz()
	{
		return Object.class;
	}

}
